// pages/AppointmentRecord/AppointmentRecord.js
var request = require("../../utils/request.js")
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabTitle: ['未开始', '已结束'],
    width: 20,
    chooseSize: false,
    animationData: {},
    showModal: false,
    msg: "",
    appointMsg: "",
    appoinmentstartlist: [],
    appoinmentendlist: [],
    appoinmentName: "请选择",
    times: "请选择",
    clinicName: "",
    project: "",
    doctorName: "",
    doctorList: [],
    projectList: [],
    userList: [],
    userId: "",
    multiIndex: [0, 0],
    multiArray: [],
    aid: "", //预约记录id
    submitValue: "",
    s:"0",
    m:"0",
    info:"",
    info1:""
  },
  //修改预约
  chooseSezi: function (e) {
    var item = e.currentTarget.dataset.item
    // 用that取代this，防止不必要的情况发生
    var that = this;
    that.toastDialog.showDialog({
      code: 'loading',
      message: '加载中',
      timeout: false
    }); //loading 加载框
    request.postData('xcx/appointmentData', {
      aid: item.id
    }, function (res) {
      that.toastDialog.hideDialog(); //关闭loading提示框
      if (res.code == 0) {
        // 创建一个动画实例
        var animation = wx.createAnimation({
          // 动画持续时间
          duration: 500,
          // 定义动画效果，当前是匀速
          timingFunction: 'linear'
        })
        // 将该变量赋值给当前动画
        that.animation = animation
        // 先在y轴偏移，然后用step()完成一个动画
        animation.translateY(200).step()
        // 用setData改变当前动画
        that.setData({
          // 通过export()方法导出数据
          animationData: animation.export(),
          // 改变view里面的Wx：if
          chooseSize: true
        })
        // 设置setTimeout来改变y轴偏移量，实现有感觉的滑动
        setTimeout(function () {
          animation.translateY(-400).step()
          that.setData({
            animationData: animation.export()
          })
        }, 200)
        that.setData({
          appoinmentName: res.data.name,
          clinicName: res.data.clinicName,
          times: res.data.dateStr,
          project: res.data.project,
          doctorName: res.data.doctorName,
          doctorId: res.data.doctorId,
          userId: res.data.userId,
          aid: res.data.id
        })
        that.ReservationForm(e.currentTarget.dataset.item)
        that.getDoctorTime(e.currentTarget.dataset.item)
      } else {
        that.toastDialog.showDialog({
          code: res.code,
          message: res.message,
          timeout: true
        });
      }
    }, function (res) {
      that.toastDialog.showDialog({
        code: res.code,
        message: res.message,
        timeout: true
      });
    });

  },
  hideModal: function (e) {
    var that = this;
    var animation = wx.createAnimation({
      duration: 1000,
      timingFunction: 'linear'
    })
    that.animation = animation
    animation.translateY(200).step()
    that.setData({
      animationData: animation.export()

    })
    setTimeout(function () {
      animation.translateY(-400).step()
      that.setData({
        animationData: animation.export(),
        chooseSize: false
      })
    }, 200)
  },
  //提交修改
  saveBtn: function (e) {
    this.hideModal()
    this.setData({
      msg: "注意：修改预约只可提交一次！",
      appointMsg: "修改预约",
      showModal: true
    })
  },
  modalConfirm: function (e) {
    console.log(e);
    var that = this;
    var a = that.data.submitValue
    
    that.toastDialog.showDialog({
      code: 'loading',
      message: '加载中',
      timeout: false
    }); //loading 加载框
    if(e.detail.text == "确定取消"){
      request.postData('xcx/cancelAppointment', {"aid":that.data.aid}, function (res) {
        that.toastDialog.hideDialog(); //关闭loading提示框
        console.log(res);
        if (res.code == 0) {
          that.appoinmentStart();
          that.appoinmentEnd();
        } else {
          that.toastDialog.showDialog({
            code: res.code,
            message: res.message,
            timeout: true
          });
        }
      }, function (res) {
        that.toastDialog.showDialog({
          code: res.code,
          message: res.message,
          timeout: true
        });
      });
    }else{
      request.postData('xcx/updateAppointment', a, function (res) {
        that.toastDialog.hideDialog(); //关闭loading提示框
        if (res.code == 0) {
          that.appoinmentStart();
        } else {
          that.toastDialog.showDialog({
            code: res.code,
            message: res.message,
            timeout: true
          });
        }
      }, function (res) {
        that.toastDialog.showDialog({
          code: res.code,
          message: res.message,
          timeout: true
        });
      });
    }
  
  },
  formSubmit: function (e) {
    console.log(e);
    for(var i = 0;i<this.data.doctorList.length;i++){
      if(this.data.doctorList[i].name == e.detail.value.doctorId){
        e.detail.value.doctorId = this.data.doctorList[i].id
      }
    }
    this.setData({
      submitValue: e.detail.value
    })
  },
  backBtn: function () {
    this.hideModal()
    this.setData({
      msg: "您确定要取消当前预约吗？",
      appointMsg: "确定取消",
      showModal: true
    })
  },
  //状态切换
  TabClick: function (e) {
    if (e.detail.num == 0) {
      this.appoinmentStart();
    } else if (e.detail.num == 1) {
      this.appoinmentEnd();
    }
  },
  //未开始记录
  appoinmentStart: function () {
    var that = this;
    that.toastDialog.showDialog({
      code: 'loading',
      message: '加载中',
      timeout: false
    }); //loading 加载框
    request.postData('xcx/appointmentList', {}, function (res) {
     
      console.log(res)
      if (res.code == 0) {
        that.setData({
          appoinmentstartlist: res.list,
        })
        if(res.list.length == 0){
          that.setData({
            info:"暂无数据~",
            s:"1",
            m:"1"
          })
        }
        that.toastDialog.hideDialog(); //关闭loading提示框
       
      } else {
        that.toastDialog.showDialog({
          code: res.code,
          message: res.message,
          timeout: true
        });
      }
    }, function (res) {
      that.toastDialog.showDialog({
        code: res.code,
        message: res.message,
        timeout: true
      });
    });
  },
  //已完成记录
  appoinmentEnd: function () {
    var that = this;
    that.toastDialog.showDialog({
      code: 'loading',
      message: '加载中',
      timeout: false
    }); //loading 加载框
    request.postData('xcx/appointmentCompleteList', {}, function (res) {
      
      if (res.code == 0) {
        that.toastDialog.hideDialog(); //关闭loading提示框
        if(res.list.length == 0){
          that.setData({
            info:"暂无数据~",
            s:"1",
            m:"1"
          })
        }else{
          that.setData({
            appoinmentendlist: res.list
          })
        }
        
      } else {
        that.toastDialog.showDialog({
          code: res.code,
          message: res.message,
          timeout: true
        });
      }
    }, function (res) {
      that.toastDialog.showDialog({
        code: res.code,
        message: res.message,
        timeout: true
      });
    });
  },
  //预约获取基本信息
  ReservationForm: function (option) {
    var that = this;
    that.toastDialog.showDialog({
      code: 'loading',
      message: '加载中',
      timeout: false
    }); //loading 加载框
    request.postData('xcx/appointmentInfo', {
      cid: option.clinicId
    }, function (res) {
      that.toastDialog.hideDialog(); //关闭loading提示框
      if (res.code == 0) {
        var p = [];
        for (var i = 0; i < res.projectList.length; i++) {
          var r = {
            "id": i,
            "name": res.projectList[i]
          }
          p.push(r)
        }
        that.setData({
          doctorList: res.doctorList,
          projectList: p,
          userList: res.userList,
        })
      } else {
        that.toastDialog.showDialog({
          code: res.code,
          message: res.message,
          timeout: true
        });
      }
    }, function (res) {
      that.toastDialog.showDialog({
        code: res.code,
        message: res.message,
        timeout: true
      });
    });
  },
  //获取医生出诊时间
  getDoctorTime: function (option) {
    var that = this;
    that.toastDialog.showDialog({
      code: 'loading',
      message: '加载中',
      timeout: false
    }); //loading 加载框
    request.postData('xcx/clinicDoctorTime', {
      cdId: option.clinicId
    }, function (res) {
      that.toastDialog.hideDialog(); //关闭loading提示框
      if (res.code == 0) {
        var day = [];
        var hours = [];
        var temp = [];
        for (var i = 0; i < res.timeList.length; i++) {
          day.push(res.timeList[i].day);

        }
        for (var j = 0; j < res.timeList[0].hourList.length; j++) {
          hours.push(res.timeList[0].hourList[j].start + "-" + res.timeList[0].hourList[j].end);
        }
        that.setData({
          multiArray: [day, hours]
        })
      } else {
        that.toastDialog.showDialog({
          code: res.code,
          message: res.message,
          timeout: true
        });
      }
    }, function (res) {
      that.toastDialog.showDialog({
        code: res.code,
        message: res.message,
        timeout: true
      });
    });
  },
  //选择时间段
  bindMultiPickerChange: function (e) {
    this.setData({
      multiIndex: e.detail.value
    })
    const index = this.data.multiIndex;
    const year = this.data.multiArray[0][index[0]];
    const month = this.data.multiArray[1][index[1]];
    this.setData({
      times: year + " " + month,
      multiIndex: [0, 0]
    })
  },
  change: function (e) {
    this.setData({
      userId: e.detail
    })
  },
  change1: function (e) {
    this.setData({
      project: this.data.projectList[e.detail].name
    })


  },
  //预约医生选择 获取预约时间列表
  change2: function (e) {
    // console.log(e);
    // console.log(this.data.doctorList)
    var names = "";
    for(var i = 0;i<this.data.doctorList.length;i++){
      if(this.data.doctorList[i].id == e.detail){
        names = this.data.doctorList[i].name
      }
    }
    this.setData({
      doctorName: names,
      isPicker: false
    })
    this.getDoctorTime({
      clinicId: e.detail
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   // console.log(options)
   wx.showShareMenu({ menus: ['shareAppMessage', 'shareTimeline']})
    this.toastDialog = this.selectComponent("#toastDialog"); //此处id和页面的 wxml中的id一致
    this.appoinmentStart();
  },
 /**
   * 用户点击右上角分享
   */
  onShareAppMessage (res) {
    return {
      title: '迪佳极视',
      path: 'pages/index/index',
      // imageUrl:'这个是显示的图片，不写就默认当前页面的截图',
      success: function (shareTickets) {
        console.info(shareTickets + '成功');
        // 转发成功
      },
      fail: function (res) {
        console.log(res + '失败');
        // 转发失败
      },
      complete:function(res){
        // 不管成功失败都会执行
      }
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    wx.switchTab({
      url: '../mine/mine'
    });
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})